package com.wkbb.basic.controller.manage;

import com.github.pagehelper.PageInfo;
import com.wkbb.basic.service.BlackListService;
import com.wkbb.common.base.BaseController;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.constants.VersionConstant;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.BlacklistInfoDto;
import com.wkbb.common.dto.basic.UserDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lizhichao
 * @date 2019/8/9
 * @description
 */
@RestController
@Api(description = "黑名单管理")
@RequestMapping("/basic/{apiVersion}/blacklist/manage")
@Slf4j
public class BlacklistManageController extends BaseController {

    @Autowired
    private BlackListService blackListService;

    @ApiOperation("查询黑名单分页列表")
    @RequestMapping(value = "/getBlacklistInfoList", method = RequestMethod.GET)
    public ResultDTO<PageInfo<BlacklistInfoDto>> getBlacklistInfoList(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion") String apiVersion,
            @ApiParam(value = "组织id", required = true) @RequestParam(name = "orgId", required = true) Long orgId,
            @ApiParam(value = "姓名", required = false) @RequestParam(name = "name", required = false) String name,
            @ApiParam(value = "加黑原因", required = false) @RequestParam(name = "reason", required = false) String reason,
            @ApiParam(value = "预警等级", required = false) @RequestParam(name = "warnLevel", required = false) String warnLevel,
            @ApiParam(value = "页码",required = false,defaultValue = "1") @RequestParam(name = "pageNo",required = false,defaultValue = "1") Integer pageNo,
            @ApiParam(value = "每页条数",required = false,defaultValue = "10") @RequestParam(name = "pageSize",required = false,defaultValue = "10") Integer pageSize) {

        log.info("***********BlacklistController.getBlacklistInfoList******...apiVersion:{},name:{},reason:{},warnLevel:{}", apiVersion, name,reason,warnLevel);
        UserDto userDto = getUserByLogin();
        if (userDto == null) {
            return new ResultDTO<>(ErrorCode.USER_NOT_EXIST);
        }
        try {
            Map<String,Object> parms = new HashMap<>();
            parms.put("orgId", orgId);
            if(name!=null){
                parms.put("name",name);
            }
            if(reason!=null){
                parms.put("reason",reason);
            }
            if(warnLevel!=null){
                parms.put("warnLevel",warnLevel);
            }
            return new ResultDTO(blackListService.getBlacklistInfoList(parms,pageNo,pageSize));
        } catch (Exception e) {
            log.error("***********BlacklistController.getBlacklistInfoList******查询黑名单分页列表异常", e);
            return new ResultDTO<>(false);
        }
    }

    @ApiOperation("新增黑名单")
    @RequestMapping(value = "/addBlacklist", method = RequestMethod.POST)
    public ResultDTO<Object> addBlacklist(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion") String apiVersion,
            @ApiParam(value = "黑名单") @RequestBody BlacklistInfoDto blacklistInfoDto){

        log.info("***********BlacklistController.addBlacklist******...apiVersion:{},blacklistInfoDto:{}", apiVersion, blacklistInfoDto);

        UserDto userDto = getUserByLogin();
        if (userDto == null) {
            return new ResultDTO<>(ErrorCode.USER_NOT_EXIST);
        }
        if(blacklistInfoDto == null || blacklistInfoDto.getWarnLevel() == null || blacklistInfoDto.getHeadImgUrl() == null){
            return new ResultDTO<>(ErrorCode.PARAM_MISSING);
        }

        try {
            return blackListService.insert(blacklistInfoDto,userDto.getName());
        } catch (Exception e) {
            log.error("***********BlacklistController.addBlacklist******新增黑名单异常", e);
            return new ResultDTO<>(false);
        }
    }

    @ApiOperation("删除黑名单")
    @RequestMapping(value = "/deleteBlacklist", method = RequestMethod.DELETE)
    public ResultDTO deleteBlacklist(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion") String apiVersion,
            @ApiParam(value = "黑名单id集合", required = true) @RequestParam(name = "id", required = true) List<Long> ids){

        log.info("***********BlacklistController.deleteBlacklist******...apiVersion:{},ids", apiVersion, ids);

        UserDto userDto = getUserByLogin();
        if (userDto == null) {
            return new ResultDTO<>(ErrorCode.USER_NOT_EXIST);
        }

        try {
            blackListService.delete(ids);
            return new ResultDTO<>(true);
        } catch (Exception e) {
            log.error("***********BlacklistController.addBlacklist******删除黑名单异常", e);
            return new ResultDTO<>(false);
        }

    }

    @ApiOperation("编辑黑名单")
    @RequestMapping(value = "/updateBlacklist", method = RequestMethod.PUT)
    public ResultDTO updateBlacklist(
            @ApiParam(value = "接口版本", defaultValue = VersionConstant.V1, required = true) @PathVariable(name = "apiVersion") String apiVersion,
            @ApiParam(value = "黑名单") @RequestBody BlacklistInfoDto blacklistInfoDto){

        log.info("***********BlacklistController.updateBlacklist******...apiVersion:{},blacklistInfoDto:{}", apiVersion, blacklistInfoDto);
        UserDto userDto = getUserByLogin();
        if (userDto == null) {
            return new ResultDTO<>(ErrorCode.USER_NOT_EXIST);
        }
        if(blacklistInfoDto == null || blacklistInfoDto.getId() == null ||
                blacklistInfoDto.getWarnLevel() == null || blacklistInfoDto.getHeadImgUrl() == null){
            return new ResultDTO<>(ErrorCode.PARAM_MISSING);
        }

        try {
            return blackListService.update(blacklistInfoDto,userDto.getName());
        } catch (Exception e) {
            log.error("***********BlacklistController.addBlacklist******编辑黑名单异常", e);
            return new ResultDTO<>(false);
        }
    }

}
